package com.huiminpay.transaction.controller;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeWapPayRequest;
import com.alipay.api.response.AlipayTradeWapPayResponse;
import com.huiminpay.transaction.api.dto.QRCodeDto;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Random;

@Controller
public class TestController {

    @GetMapping("alipay.html")
    public void alipay(HttpServletResponse response) throws AlipayApiException, IOException {
        // 属于公共请求参数
        String serverUrl = "https://openapi.alipaydev.com/gateway.do",appId="2016091600522562",format="json",charset="utf-8",signType="RSA2";
        String privateKey = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCEf4ZUcIZHuIseJ63IOafMN+a1WblWfKnuPtEwNYpdoRDm/kCo9i+bggxI/IA0T5TmfgrrTjgnzzOVJH7SFXsY8Rc9MYe0msNQvSsdikI+FOjnSmXylhMhtKycrIPTw3mGzD5MMaPpCLOGtRi+Ab7Y6005BZzJ/C0glHPTqXyJbOTkOFjVkZ8VuQAsdrL7bbYYR3A8rV1C6gXK6QfmiTyqmxvDoSGQOzGiT7NiGQqwu1CspYIV7ch0iCAVYp6vkkKInv0AETn93S1obLrRwsBkbc95TJHu8BrElaxpW02Uj4i5H7gp2UWKYDKjJnsS0eit4EsGGVyVMVy+kxpJhH4RAgMBAAECggEAaNQoaGg6pUXps2r7QF31V0qnGrYpP7cUDy3d/cFufpNvNwLUD5nvop5qr9guarTH/vCcHP1eyohRrF8ag4aBRQOCOmUUlByaCSgRk6aOsGWEu3GOZTa9Xj5/Dtoi1oliffbcFxZzM20x9JMVR0dEQoUwLLw3B0ugPJWXalA++s5sKXPVPEgVcwZqTSeNZH/P4FKpT6T9OL7DRErdmwjxRCN2wsBkgmoxGtcx7aPvdbMJlGOZkQkSq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h82SIVrlxHUaiPthmzv688eo2CEbxzcQr8ahtMmwBbUvwglbjKmCSsGFauKHNosvziCRsGthr/tx2fkMcZCy7LI7hwiUPgMmYw1eJ6f297S6Clp8GeMlOOoN/f3oATjOd77KNoliA6gjpzkEu+44Bftfqzcl/TwWWKWA34GiKGa7Uu7mEkaflPsb/nmaEXAqSXYcRt+mXIIKBZj1TVXAUxcqEhmLc26McmxTbEe2CQDXUHruxYPGNd9xd36ihC/jp1S5O6tmw2bcAnMNZq21Y81yKyy+hywd9sWApQIDAQAB";
        AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset,
                alipayPublicKey, signType);

        AlipayTradeWapPayRequest request = new AlipayTradeWapPayRequest();
        request.setNotifyUrl("");
        request.setReturnUrl("");
        // 业务参数
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", "T"+System.currentTimeMillis());
        bizContent.put("total_amount", new Random().nextInt(1000));
        bizContent.put("subject", "Iphone13Pro max 1PB 深邃蓝");
        bizContent.put("product_code", "QUICK_WAP_WAY");

        request.setBizContent(bizContent.toString());
        AlipayTradeWapPayResponse alipayTradeWapPayResponse = alipayClient.pageExecute(request);
        response.setContentType("text/html;charset="+charset);
        if (alipayTradeWapPayResponse.isSuccess()) {

            response.getWriter().write(alipayTradeWapPayResponse.getBody());
            System.out.println("调用成功");
        } else {
            System.out.println("调用失败");
            response.getWriter().write("调用支付宝失败");
        }

    }

    @GetMapping("index.html")
    public String index(Model model){
        QRCodeDto qrCodeDto = new QRCodeDto();
        qrCodeDto.setBody("你好");
        qrCodeDto.setSubject("这是中国第一号店铺");
        model.addAttribute("qr",qrCodeDto);

        return "index";
    }
}
